package lv.ante.xwiki.cli;

/**
 * Enables reading program output by lines. 
 * @author kap
 *
 */
public abstract class LineResponseHandler implements ResponseStreamHandler {
	StringBuffer lineBuf = new StringBuffer();

	public void handle(int nextChar) {
	        //System.out.print((char)nextChar);
		if (nextChar == '\n') {
			String line = lineBuf.toString();
			handle(line);
			lineBuf.delete(0, lineBuf.length());
		} else {
			lineBuf.append((char) nextChar);
		}

	}
	
	/**
	 * If the output does not terminate with \n
	 */
	public String getRemainingLine() {
	    return lineBuf.toString();
	}

	public abstract void handle(String line);
}
